import { fetch } from '../services/menus';
import { parse } from 'qs';
import * as commonService from '../services/CommonCRUD';
export default {
    namespace: 'menus',
    state: {
        userID: null,
        orgID: null,
        pOrgID: null,
        userName: null,
        userNo: null,
        odsDate: null,
        odsDatesy: null,
        cmDate: null,
        idIndexMap: {},
        popedomTypes: {},
        menus: [],
        parents: {},
        current: '0',
        hearderId: '0',
        panes: [],
        loading: false,
        total: 0,
        offsetWidth: 1366,
        offsetHeight: 631,
        collapsedLeftMenu: false,
        displayPwd: false,
        openkeys: [],
        reptQue: [],
        sjy: null,
        iscsh: null,
        lsghr: null,
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(location => {
                if (location.pathname === '/') {
                    dispatch({
                        type: 'query',
                        payload: location.query,
                    });
                } else if (location.pathname === '/login') {
                    dispatch({
                        type: 'queryLogin',
                        payload: location.queryLogin,
                    });
                } else if (location.pathname === '/error') {
                    dispatch({
                        type: 'queryLogin',
                        payload: location.queryLogin,
                    });
                }
            });
        },
    },
    effects: {
        *queryLogin({ payload }, { call, put }) {
            yield put({ type: 'showLoading' });
        },
        *query({ payload }, { call, put }) {
            yield put({ type: 'showLoading' });
            const data = yield call(fetch);
            if (data) {
                yield put({
                    type: 'querySuccess',
                    payload: {
                        sysInfo: data
                    },
                });
            }
        }
    },
    reducers: {
        setUser(state, { payload: { userID, orgID, pOrgID, sjy, iscsh } }) {
            return { ...state, userID, orgID, pOrgID, sjy, iscsh };
        },
        showLoading(state) {
            return { ...state, loading: true };
        },
        selectHeaderChange(state, action) {
            return { ...state, ...action.payload };
        },
        selectMenuChange(state, action) {
            return { ...state, ...action.payload };
        },
        setOffset(state, action) {
            return { ...state, ...action.payload };
        },
        setCollapsedLeftMenu(state, action) {
            return { ...state, ...action.payload };
        },
        hidePwd(state) {
            return { ...state, displayPwd: false };
        },
        addPane(state, action) {
            let ps = state.panes;
            let { id, name, comp, props, index } = action.payload;
            let fIndex = -1;
            for (let i = 0; i < ps.length; i++) {
                if (ps[i].index === index) {
                    fIndex = i;
                    break;
                }
            }
            const key = id
            commonService.setUseLog({ key })
            if (fIndex === -1) {
                let newPanes = ps.concat({ id, name, comp, props, index });
                return { ...state, ...{ panes: newPanes, current: index } };
            } else {
                return { ...state, ...{ current: index } };
            }
        },
        deletePane(state, action) {
            let ps = state.panes;
            let { index } = action.payload;
            let lastIndex;
            for (let i = 0; i < ps.length; i++) {
                if (ps[i].index === index) {
                    lastIndex = i - 1;
                    break;
                }
            }
            const newPanes = ps.filter(p => p.index !== index);
            let cur = state.current;
            if (lastIndex >= 0 && cur === index) {
                cur = ps[lastIndex].index;
            }
            return { ...state, ...{ panes: newPanes, current: cur } };
        },
        deleteAllPane(state) {
            const newPanes = [];
            let cur = '0';
            return { ...state, ...{ panes: newPanes, current: cur } };
        },
        deleteOtherPane(state) {
            let ps = state.panes;
            let pane = {};
            for (let i = 0; i < ps.length; i++) {
                if (ps[i].index === state.current) {
                    pane = ps[i];
                    break;
                }
            }
            const newPanes = [];
            newPanes.push(pane);
            let cur = state.current;
            return { ...state, ...{ panes: newPanes, current: cur } };
        },
        deleteCurrentPane(state) {
            let ps = state.panes;
            let index = state.current;
            let lastIndex;
            for (let i = 0; i < ps.length; i++) {
                if (ps[i].index === index) {
                    lastIndex = i - 1;
                    break;
                }
            }
            const newPanes = ps.filter(p => p.index !== index);
            let cur = state.current;
            if (lastIndex >= 0 && cur === index) {
                cur = ps[lastIndex].index;
            }
            return { ...state, ...{ panes: newPanes, current: cur } };
        },
        initMenus(state) {
            const newPanes = [];
            let hearderId = '0';
            let cur = '0';
            return { ...state, ...{ panes: newPanes, hearderId: hearderId, current: cur } };
        },
        addReptQueItem(state, { payload: { permID } }) {
            const reptQue = [...state.reptQue, permID]
            return { ...state, reptQue }
        },
        delReptQueItem(state, { payload: { permID } }) {
            const [, newReptQue] = state.reptQue
            let reptQue = newReptQue
            if (!reptQue) {
                reptQue = []
            }
            return { ...state, reptQue }
        },
        querySuccess(state, action) {
            let pId = -1;
            let childIds = [];
            let parents = {};
            let popedomTypes = {};
            let idIndexMap = {};
            const sysInfo = action.payload.sysInfo
            sysInfo.menus.map((m, i) => {
                if (m.ParentID !== pId) {
                    childIds = new Array();
                    parents[m.ParentID] = childIds;
                    pId = m.ParentID;
                }
                idIndexMap[m.ID] = i
                childIds.push(i)
                popedomTypes[m.ID] = m.UserPopedomTypes
            })
            action.payload.parents = parents;
            //  if (orgID==61 ||pOrgID==61){
            //     return {  ...sysInfo.odsDate };
            // }else{
            //   return { ...state, parents, loading: false, ...sysInfo, idIndexMap,popedomTypes };

            //  }


            return { ...state, parents, loading: false, ...sysInfo, idIndexMap, popedomTypes };
        }
    }
};
